package top.meeting.room.module.book.listener;

import net.dreamlu.event.core.ApplicationListener;
import net.dreamlu.event.core.Listener;
import top.meeting.room.common.kit.WxKit;
import top.meeting.room.module.book.event.BookMeetEvent;
import top.meeting.room.module.book.model.Book;
import top.meeting.room.module.system.model.User;
import top.meeting.room.module.system.services.impl.UserService;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 我有故事，你有酒么？
 * JKhaled created by yunqisong@foxmail.com 2017/9/17
 * FOR : 预定会议室成功监听
 */
@Listener(order = 1, enableAsync = true)
public class PushMsgToAppover implements ApplicationListener<BookMeetEvent> {

    @Override
    public void onApplicationEvent(BookMeetEvent event) {
        //给人事发送推送
        List<User> userList = UserService.me.findSp();

        if ("".equals(event.getSource())) {
            WxKit.sendMessage(event.getStr(),
                    (userList == null || userList.isEmpty()) ? Arrays.asList() : userList.stream().map(user -> user.getOpenId())
                            .collect(Collectors.toList()));
            return;
        }
        Book book = (Book) event.getSource();
        String str = event.getStr();
        String msg = null;
        if ("审批预订".equals(str)) {
            msg = "有新的会议室预定需要审批:\n";
        }
        if ("提醒开门".equals(str)) {
            msg = "有一个会议即将开始，请提前准备好会议室:\n";
        }
        WxKit.sendMessage(book.msgForBookApprover(book.getId(), msg),
                (userList == null || userList.isEmpty()) ? Arrays.asList() : userList.stream().map(user -> user.getOpenId()).collect(Collectors.toList()));

    }
}
